package com.ruoyi.yipaiju.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.common.core.domain.entity.XcxUser;
import com.ruoyi.common.utils.CurrencyUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.yipaiju.domain.DiscountHouse;
import com.ruoyi.yipaiju.domain.HouseUploadingOrder;
import com.ruoyi.yipaiju.service.IDiscountHouseService;
import com.ruoyi.yipaiju.service.IHouseUploadingOrderService;
import com.ruoyi.yipaiju.service.IStorageRelationshipService;
import com.ruoyi.yipaiju.service.IXcxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/houses")
public class HousesUploadingController extends BaseController {


    @Autowired
    private IHouseUploadingOrderService houseUploadingOrderService;
    @Autowired
    private IDiscountHouseService discountHouseService;

    @Autowired
    private IStorageRelationshipService storageRelationshipService;
    @Autowired
    private IXcxUserService xcxUserService;

    /**
     * 放盘签约
     * @param houseUploadingOrder
     * @return
     */
    @PostMapping("/uploading/orders")
    public ApiResult uploadingOrders(@RequestBody HouseUploadingOrder houseUploadingOrder){
        houseUploadingOrder.setNo(CurrencyUtil.create(10));
        // 客户是传进来的 不是当前的登录用户  // 这里可能是空的  如果空的在放盘审核里可以分享， 不知道是什么逻辑
//        houseUploadingOrder.setClient(SecurityUtils.getUserId().toString());
        // TODO 需要设置marketer 但不知道应该是什么  放盘签约的接单人 在完成放盘预约的时候更新上   这个就当前的登录用户 也就是 放盘签约的市场专员
        houseUploadingOrder.setMarketer(SecurityUtils.getUserId());
        houseUploadingOrder.setSubmitTime(new Date());
        // TODO 设置市场经理 和当前用户同一个组下的市场经理
        XcxUser xcxUser = xcxUserService.selectOneXcxUser(Builder.of(XcxUser::new)
                .with(XcxUser::setGroup_id, SecurityUtils.getLoginUser().getXcxUser().getGroup_id())
                .with(XcxUser::setActive_role_id, 2L)  // 这里传市场经理
                .with(XcxUser::setRole_code, "renter_marketing_manager")  // 这里传市场经理
                .build());
        if (xcxUser != null){
            houseUploadingOrder.setMarketManager(xcxUser.getId().toString());
        }
        /**
         * 1、客户申请放盘，生成放盘预约订单
         * 2、专员接单（市场专员或合伙人专员），和客户沟通后完成线下服务工作，生成放盘签约订单，同时生成对应客户的房源
         * 3、市场经理对专员提交的签约订单进行审核，审核通过后房源上架
         */
        houseUploadingOrder.setMarketManagerVertifyStatus(1);
        // 直接生成一个折扣房源
        DiscountHouse discountHouse = new DiscountHouse();
        discountHouse.setUploadTime(new Date());
        discountHouseService.insertDiscountHouse(discountHouse);
        houseUploadingOrder.setDiscountHouseId(discountHouse.getId());
        houseUploadingOrderService.insertHouseUploadingOrder(houseUploadingOrder);
        return ApiResult.success(houseUploadingOrder);
    }

    /**
     * 放盘签约订单详情
     * @param id
     * @return
     */
    @GetMapping("/uploading/orders/{id}")
    public ApiResult uploadingOrdersInfo(@PathVariable("id") Long id){
        HouseUploadingOrder houseUploadingOrder = houseUploadingOrderService.selectHouseUploadingOrderById(id);
        if (houseUploadingOrder != null){
            //获取市场专员 和 市场经理信息
            if (houseUploadingOrder.getMarketer() != null){
                houseUploadingOrder.setMarketerUser(xcxUserService.selectXcxUserById(houseUploadingOrder.getMarketer()));
            }
            if (StringUtils.isNotEmpty(houseUploadingOrder.getMarketManager())){
                houseUploadingOrder.setMarketManagerUser(xcxUserService.selectXcxUserById(Long.parseLong(houseUploadingOrder.getMarketManager())));
            }

            DiscountHouse discountHouse = discountHouseService.selectDiscountHouseById(houseUploadingOrder.getDiscountHouseId());
            if (discountHouse != null){
                if (StringUtils.isNotEmpty(discountHouse.getTagsstr())){
                    discountHouse.setTags(discountHouse.getTagsstr().split(","));
                }
                houseUploadingOrder.setDiscountHouse(discountHouse);
            }else {
                houseUploadingOrder.setDiscountHouse(new DiscountHouse());
            }
            return ApiResult.success(houseUploadingOrder);
        }
        return ApiResult.error();
    }

}
